$(function () {
    var layer = layui.layer
    var form = layui.form
    var laypage = layui.laypage

    function padZero(n) {
        return n < 10 ? '0' + n : n
    }
    template.defaults.imports.dataFormat = function (data) {
        const dt = new Date()
        var y = dt.getFullYear()
        var m = padZero(dt.getMonth() + 1)
        var d = padZero(dt.getDate())
        var hh = padZero(dt.getHours())
        var mm = padZero(dt.getMinutes())
        var ss = padZero(dt.getSeconds())
        return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
    }
    var q = {
        pagenum: 1, // 页码值，默认请求第一页的数据
        pagesize: 2, // 每页显示几条数据，默认每页显示2条
        cate_id: '', // 文章分类的 Id
        state: '' // 文章的发布状态
    }
    initCate()
    initTable()
    function initTable() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('获取文章列表失败！')
                }
                layer.msg('获取文章列表成功！')
                var htmlStr = template('tpl-list', res)
                $('#listbox').html(htmlStr)
            }
        })
    }

    // 初始化文章分类的方法
    function initCate() {
        $.ajax({
            method: 'GET',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('初始化失败')
                }
                layer.msg('初始化成功')
                var htmlStr = template('tpl-cate', res)
                $('tbody').html(htmlStr)

                // 调用渲染分页的方法
                renderPage(res.total)
                form.render()

            }
        })
    }
    $('#form-search').on('submit', function (e) {
        e.preventDefault()
        var status = $([name = status]).val()
        var cate_id = $([name = cate_id]).val()
        q.cate_id = cate_id
        q.status = status
        initTable()
    })
    // 定义渲染分页的方法
    function renderPage(total) {
        laypage.render({
            elem: 'pageBox',
            count: total,
            limit: q.pagesize,
            curr: q.pagenum,
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [2, 3, 5, 10],
            jump: function (obj, first) {
                q.pagenum = obj.curr
                q.pagesize = obj.limit
                if (!first) {
                    initTable()
                }

            }
        });
    }
    // 编辑
    var editData = null
    $('tbody').on('click', '.btn-edit', function () {
        editData = layer.open({
            type: 1,
            title: '编辑文章',
            area: ['500px', '300px'],
            content: $('#bianji').html()
        });
        var id = $(this).attr('data-id')
        $.ajax({
            method: 'GET',
            url: '/my/article/' + id,
            success: function (res) {
                form.val('editBox', res.data)
            }
        })
    })
    $('body').on('submit', '#editBox', function (e) {
        e.preventDefault()
        console.log($(this));
        $.ajax({
            method: 'POST',
            url: '/my/article/edit',
            data: $(this).serialize(),
            success: function (res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg('更新列表失败')
                }
                layer.msg('更新成功')
                layer.close(editData)
                initCate()
            }
        })
    })
    // 删除
    $('tbody').on('click', '#remove', function () {
        var len = $('#remove').length
        var id = $(this).attr('data-id')
        layer.confirm('确定要删除吗', { icon: 3, title: '提示' }, function (index) {
            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + id,
                success: function (res) {
                    if (res.status !== 0) {
                        return layer.msg('删除文章失败')
                    }
                    layer.msg('删除成功')
                    if (len === 1) {
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1
                    }
                    initTable()
                    renderPage(q.pagenum)
                }
            })

            layer.close(index);
        });


    })
})
